package com.primb.bench.common.viewmodel;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.primb.bench.R;
import com.zhy.autolayout.AutoLinearLayout;

/**
 * Created by Chen on 2017/5/10.
 * 功能描述：键值对加图标类型数据驱动模型
 */

public class ViewIconKVEntity implements Parcelable {
    String key;
    String value;
    public static int[] ICONS = {R.mipmap.calculate_icon_1, R.mipmap.calculate_icon_2, R.mipmap.calculate_icon_3};
    public static int position = 0;
    ImageView imageEdit;

    public ImageView getImageEdit() {
        return imageEdit;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void addView(Context context, ViewGroup viewGroup, boolean isImageView) {
        AutoLinearLayout layout = new AutoLinearLayout(context);
        AutoLinearLayout.LayoutParams layoutParams = new AutoLinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layout.setPadding(0, 17, 0, 17);
        layout.setOrientation(LinearLayout.HORIZONTAL);
        layout.setLayoutParams(layoutParams);

        ImageView imageView = new ImageView(context);
        imageView.setVisibility(View.VISIBLE);
        AutoLinearLayout.LayoutParams paramsIcon = new AutoLinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        paramsIcon.gravity = Gravity.CENTER_VERTICAL;
        imageView.setImageResource(ICONS[position % ICONS.length]);
        position++;
        paramsIcon.setMargins(0, 0, 20, 0);
        imageView.setLayoutParams(paramsIcon);
        layout.addView(imageView);


        TextView tvKey = new TextView(context);
        tvKey.setGravity(Gravity.LEFT);
        tvKey.setTextSize(13);
        tvKey.setTextColor(ContextCompat.getColor(context, R.color.calculate_title_font));
        if (key != null)
            tvKey.setText(key + ":");
        AutoLinearLayout.LayoutParams paramsLeft = new AutoLinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        paramsLeft.gravity = Gravity.CENTER_VERTICAL;
        tvKey.setPadding(0, 0, 10, 0);
        tvKey.setLayoutParams(paramsLeft);
        layout.addView(tvKey);

        TextView tvValue = new TextView(context);
        tvValue.setTextSize(13);
        tvValue.setTextColor(ContextCompat.getColor(context, R.color.calculate_title_font));
        if (value != null) {
            tvValue.setText(value);
        }
        AutoLinearLayout.LayoutParams paramsRight = new AutoLinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT);
        paramsRight.gravity = Gravity.CENTER_VERTICAL;
        paramsRight.weight = 1;
        tvValue.setLayoutParams(paramsRight);
        tvValue.setPadding(0, 0, 0, 0);
        tvValue.setMaxLines(1);
        tvValue.setEllipsize(TextUtils.TruncateAt.END);
        layout.addView(tvValue);

        if (isImageView) {
            imageEdit = new ImageView(context);
            imageEdit.setVisibility(View.VISIBLE);
            imageEdit.setPadding(33, 20, 33, 20);
            AutoLinearLayout.LayoutParams paramsEdit = new AutoLinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
            paramsEdit.gravity = Gravity.RIGHT | Gravity.TOP;
            imageEdit.setBackgroundResource(R.mipmap.calculate_edit_icon);
            paramsEdit.setMargins(0, 0, 0, 0);
            imageEdit.setLayoutParams(paramsEdit);
            layout.addView(imageEdit);
        }

        viewGroup.addView(layout);
    }

    public void addView(Context context, ViewGroup viewGroup) {
        AutoLinearLayout layout = new AutoLinearLayout(context);
        AutoLinearLayout.LayoutParams layoutParams = new AutoLinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layout.setPadding(0, 17, 0, 17);
        layout.setOrientation(LinearLayout.HORIZONTAL);
        layoutParams.gravity = Gravity.RIGHT;
        layout.setLayoutParams(layoutParams);

        TextView tvValue = new TextView(context);
        tvValue.setTextSize(13);
        tvValue.setTextColor(ContextCompat.getColor(context, R.color.calculate_title_font));
        tvValue.setText("1243");
        AutoLinearLayout.LayoutParams paramsRight = new AutoLinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT);
        paramsRight.gravity = Gravity.CENTER_VERTICAL;
        paramsRight.weight = 1;
        tvValue.setLayoutParams(paramsRight);
        tvValue.setPadding(0, 0, 0, 0);
        tvValue.setMaxLines(1);
        tvValue.setEllipsize(TextUtils.TruncateAt.END);
        tvValue.setVisibility(View.INVISIBLE);
        layout.addView(tvValue);

        imageEdit = new ImageView(context);
        imageEdit.setVisibility(View.VISIBLE);
        imageEdit.setPadding(33, 24, 33, 20);
        AutoLinearLayout.LayoutParams paramsEdit = new AutoLinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
        paramsEdit.gravity = Gravity.RIGHT | Gravity.TOP;
        imageEdit.setBackgroundResource(R.mipmap.calculate_edit_icon);
        paramsEdit.setMargins(0, 0, 0, 0);
        imageEdit.setLayoutParams(paramsEdit);
        layout.addView(imageEdit);

        viewGroup.addView(layout);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.key);
        dest.writeString(this.value);
    }

    public ViewIconKVEntity() {
    }

    public ViewIconKVEntity(String key, String value) {
        this.key = key;
        this.value = value;
    }

    protected ViewIconKVEntity(Parcel in) {
        this.key = in.readString();
        this.value = in.readString();
    }

    public static final Creator<ViewIconKVEntity> CREATOR = new Creator<ViewIconKVEntity>() {
        @Override
        public ViewIconKVEntity createFromParcel(Parcel source) {
            return new ViewIconKVEntity(source);
        }

        @Override
        public ViewIconKVEntity[] newArray(int size) {
            return new ViewIconKVEntity[size];
        }
    };
}
